package com.hs.ding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hs.bai.mapper.AdditionalGoodsinfoMapper;
import com.hs.bai.mapper.GoodsinfoMapper;
import com.hs.bai.mapper.OrderMapper;
import com.hs.bai.pojo.AdditionalGoodsinfo;
import com.hs.bai.pojo.Goodsinfo;
import com.hs.bai.pojo.Order;
import com.hs.ding.pojo.Need;
import com.hs.ding.mapper.NeedMapper;
import com.hs.ding.service.INeedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.dto.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YingFeng
 * @since 2022-06-14
 */
@Service
public class NeedServiceImpl extends ServiceImpl<NeedMapper, Need> implements INeedService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AdditionalGoodsinfoMapper additionalGoodsinfoMapper;


    /**
     * 把已提交的订单的主要信息和货物信息  插入到  需求订单表中
     * @return
     */
    @Override
    public ResultBean insertNeedList() {

        // 需求订单集合对象
        // 这个对象需要 Order +  Goodsinfo =  Need

        //需要 把 need 对象 添加到集合中
        // 把这集合添加到数据库中

        List<Need> needs = new ArrayList<>();

        //查询  订单信息
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 查询 提交的订单
        orderLambdaQueryWrapper.eq(Order::getOrderStatus,1);
        List<Order> orders = orderMapper.selectList(orderLambdaQueryWrapper);


        if (CollectionUtils.isEmpty(orders)){

            return ResultBean.ERROR("没有数据");
        }



        // 把 order 添加到 need中
        //  6
        for (Order order : orders) {
            Need need = new Need();
            need.setOrderId(order.getId());
            need.setOrderType(order.getOrderType());
            need.setTransType(order.getTransType());
            need.setCName(order.getCustomerName());
            need.setCTime(order.getOrderTime());
            need.setRoute(order.getDepartureCity()+"-"+order.getDestinationCity());
            needs.add(need);
        }


        //每个订单下面 有多个 Goodsinfo
        for (int i = 0; i < orders.size(); i++) {

            //查询 货物信息
            QueryWrapper<AdditionalGoodsinfo> additionalGoodsinfoQueryWrapper = new QueryWrapper<>();

            Order order = orders.get(i);
            System.out.println("order = " + order);
            // 根据 order id 查询 相关的 good信息 
            additionalGoodsinfoQueryWrapper.eq("order_id",order.getId());

            //查询 所有的 goods信息 
            AdditionalGoodsinfo additionalGoodsinfo = additionalGoodsinfoMapper.selectOne(additionalGoodsinfoQueryWrapper);

            Need need = needs.get(i);
            need.setGType(additionalGoodsinfo.getGoodType());
            need.setGNumber(additionalGoodsinfo.getTotalQuantity());
            need.setGWeight(additionalGoodsinfo.getTotalWeight());
            need.setGVolume(additionalGoodsinfo.getTotalVolume());
        }


        //插入操作
       //this.saveBatch(needs);
        this.saveOrUpdateBatch(needs);

        return ResultBean.OK();
    }
}
